//
//  ArticleTextViewCell.m
//  yigou
//
//  Created by wangdianlei on 2017/1/18.
//  Copyright © 2017年 hanyiwang. All rights reserved.
//

#import "ArticleTextViewCell.h"
#import "ArticleModel.h"
#import "PublishArticleViewController.h"
#import "PublishGoodViewController.h"

@implementation ArticleTextViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    self.contentTextView = [[PlaceholderTextView alloc]initWithFrame:CGRectMake(12, 10, ScreenWidth-24, 100)];
    self.contentTextView.delegate = self;
    self.contentTextView.font = [UIFont systemFontOfSize:16.f];
    self.contentTextView.textColor = [UIColor blackColor];
    self.contentTextView.textAlignment = NSTextAlignmentLeft;
    self.contentTextView.editable = YES;
    self.contentTextView.placeholderColor = [UIColor lightGrayColor];
    self.contentTextView.placeholder = @"输入详情";
    [self.contentView addSubview:self.contentTextView];
    self.contentTextView.backgroundColor = [UIColor colorWithHexString:@"E9E9F1"];
    
    CGFloat width = ScreenWidth-12 - 22;
    self.buttonmore = [UIButton buttonWithType:UIButtonTypeCustom];
    self.buttonmore.frame = CGRectMake(width - 5, 110 - 22 - 5, 22, 22);
    [self.buttonmore setBackgroundImage:[UIImage imageNamed:@"cellmore"] forState:UIControlStateNormal];
    
    self.buttondel = [UIButton buttonWithType:UIButtonTypeCustom];
    self.buttondel.frame = CGRectMake(width - 5 - (22 + 10), 110 - 22 - 5, 22, 22);
    [self.buttondel setBackgroundImage:[UIImage imageNamed:@"celldel"] forState:UIControlStateNormal];
    
    self.buttondown = [UIButton buttonWithType:UIButtonTypeCustom];
    self.buttondown.frame = CGRectMake(width - 5 - (22 + 10) * 2, 110 - 22 - 5, 22, 22);
    [self.buttondown setBackgroundImage:[UIImage imageNamed:@"celldown"] forState:UIControlStateNormal];
    
    self.buttonup = [UIButton buttonWithType:UIButtonTypeCustom];
    self.buttonup.frame = CGRectMake(width - 5 - (22 + 10) * 3, 110 - 22 - 5, 22, 22);
    [self.buttonup setBackgroundImage:[UIImage imageNamed:@"cellup"] forState:UIControlStateNormal];
    
    [self.buttonmore addTarget:self action:@selector(clickMore) forControlEvents:UIControlEventTouchUpInside];
    [self.buttondel addTarget:self action:@selector(clickDel) forControlEvents:UIControlEventTouchUpInside];
    [self.buttondown addTarget:self action:@selector(clickDown) forControlEvents:UIControlEventTouchUpInside];
    [self.buttonup addTarget:self action:@selector(clickUp) forControlEvents:UIControlEventTouchUpInside];
    
    [self.contentView addSubview:self.buttonmore];
    [self.contentView addSubview:self.buttondel];
    [self.contentView addSubview:self.buttondown];
    [self.contentView addSubview:self.buttonup];
    self.buttondel.hidden = YES;
    self.buttondown.hidden = YES;
    self.buttonup.hidden = YES;
}

- (void)textViewDidChange:(UITextView *)textView {
    if (self.controller) {
        if ([self.controller isKindOfClass:[PublishArticleViewController class]]) {
            ArticleModel *model = [PublishData sharedPublishData].localArray[self.dataIndex];
            model.txt = textView.text;
        } else if ([self.controller isKindOfClass:[PublishGoodViewController class]]) {
            ArticleModel *model = [PublishData sharedPublishData].goodlocalArray[self.dataIndex];
            model.txt = textView.text;
        }
    }
}

- (void)clickUp {
    [self clickMore];
    if (self.dataIndex == 0) {
        return;
    }
    if (self.controller) {
        if ([self.controller isKindOfClass:[PublishArticleViewController class]]) {
            PublishArticleViewController *pub = (PublishArticleViewController *)self.controller;
            [pub updateTable:self.dataIndex newIndex:self.dataIndex - 1];
        } else if ([self.controller isKindOfClass:[PublishGoodViewController class]]) {
            PublishGoodViewController *pub = (PublishGoodViewController *)self.controller;
            [pub updateTable:self.dataIndex newIndex:self.dataIndex - 1];
        }
    }
}

- (void)clickDown {
    [self clickMore];
    if (self.controller) {
        if ([self.controller isKindOfClass:[PublishArticleViewController class]]) {
            if (self.dataIndex == [PublishData sharedPublishData].localArray.count - 1) {
                return;
            }
            PublishArticleViewController *pub = (PublishArticleViewController *)self.controller;
            [pub updateTable:self.dataIndex newIndex:self.dataIndex + 1];
        } else if ([self.controller isKindOfClass:[PublishGoodViewController class]]) {
            if (self.dataIndex == [PublishData sharedPublishData].goodlocalArray.count - 1) {
                return;
            }
            PublishGoodViewController *pub = (PublishGoodViewController *)self.controller;
            [pub updateTable:self.dataIndex newIndex:self.dataIndex + 1];
        }
    }
}

- (void)clickDel {
    [self clickMore];
    if (self.controller) {
        if ([self.controller isKindOfClass:[PublishArticleViewController class]]) {
            [[PublishData sharedPublishData].localArray removeObjectAtIndex:self.dataIndex];
            
            if (self.controller) {
                PublishArticleViewController *pb = (PublishArticleViewController *)self.controller;
                [pb refreshTable];
            }
        } else if ([self.controller isKindOfClass:[PublishGoodViewController class]]) {
            [[PublishData sharedPublishData].goodlocalArray removeObjectAtIndex:self.dataIndex];
            
            if (self.controller) {
                PublishGoodViewController *pb = (PublishGoodViewController *)self.controller;
                [pb refreshTable];
            }
        }
    }
}

- (void)clickMore {
    if ([self.buttondel isHidden]) {
        self.buttondel.hidden = NO;
        self.buttonup.hidden = NO;
        self.buttondown.hidden = NO;
    } else {
        self.buttondel.hidden = YES;
        self.buttonup.hidden = YES;
        self.buttondown.hidden = YES;
    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
